﻿using System;
using Admin.Models;
using Microsoft.AspNet.Identity;
using Microsoft.AspNet.Identity.Owin;
using Microsoft.AspNet.Identity.EntityFramework;
using System.Linq;
using System.Net;
using System.Threading.Tasks;
using System.Web;
using System.Web.Mvc;
using System.Collections.Generic;
using Kendo.Mvc.Extensions;
using Kendo.Mvc.UI;
using OutDbContext;

namespace Admin.Controllers
{
    [Authorize(Roles = "Admin")]
    public class RolesAdminController : Controller
    {
        public RolesAdminController()
        {
        }

        public RolesAdminController(ApplicationUserManager userManager,
            ApplicationRoleManager roleManager)
        {
            UserManager = userManager;
            RoleManager = roleManager;
        }

        private ApplicationUserManager _userManager;
        public ApplicationUserManager UserManager
        {
            get
            {
                return _userManager ?? HttpContext.GetOwinContext().GetUserManager<ApplicationUserManager>();
            }
            set
            {
                _userManager = value;
            }
        }

        private ApplicationRoleManager _roleManager;
        public ApplicationRoleManager RoleManager
        {
            get
            {
                return _roleManager ?? HttpContext.GetOwinContext().Get<ApplicationRoleManager>();
            }
            private set
            {
                _roleManager = value;
            }
        }


        #region 角色管理
        public ActionResult Roles_Read([DataSourceRequest]DataSourceRequest request)
        {
            var result = RoleManager.Roles.Where(x => x.Name!="Admin")
                .Select(x => new RoleViewModel
                {
                    Id = x.Id,
                    Name = x.Name
                });

            return Json(result.ToDataSourceResult(request));
        }

        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Role_Create([DataSourceRequest] DataSourceRequest request, RoleViewModel role)
        {
            if (role != null && ModelState.IsValid)
            {
                var dbRole = new IdentityRole(role.Name);
                var roleresult =  RoleManager.Create(dbRole);
                if (!roleresult.Succeeded)
                {
                    ModelState.AddModelError("", roleresult.Errors.First());
                }
                else
                {
                    role.Id = dbRole.Id;
                }
            }

            return Json(new[] { role }.ToDataSourceResult(request, ModelState));
        }

        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Role_Update([DataSourceRequest] DataSourceRequest request, RoleViewModel role)
        {
            if (role != null && ModelState.IsValid)
            {
                var dbRole =  RoleManager.FindById(role.Id);
                if (dbRole != null)
                {
                    dbRole.Name = role.Name;
                    var roleresult = RoleManager.Update(dbRole);
                    if (!roleresult.Succeeded)
                    {
                        ModelState.AddModelError("", roleresult.Errors.First());
                    }
                }
            }

            return Json(new[] { role }.ToDataSourceResult(request, ModelState));
        }

        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Role_Destroy([DataSourceRequest] DataSourceRequest request, RoleViewModel role)
        {
            if (!string.IsNullOrEmpty(role?.Id))
            {
                var dbRole =  RoleManager.FindById(role.Id);
                IdentityResult result =  RoleManager.Delete(dbRole);

                if (!result.Succeeded)
                {
                    ModelState.AddModelError("", result.Errors.First());
                }
            }

            return Json(new[] { role }.ToDataSourceResult(request, ModelState));
        }
        #endregion

        //
        // GET: /Roles/
        public ActionResult Index()
        {
            ViewBag.ModuleName = "用户管理";
            ViewBag.ModuleLink = "#";
            ViewBag.PageName = "角色管理";
            ViewBag.PageTitle = "角色管理";
            return View();
        }

    }
}
